﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using System.Data.SqlClient;

namespace ivvUsuarioEdit
{
    public partial class Edit : System.Web.UI.Page
    {
        protected MetaTable table;

        protected void Page_Init(object sender, EventArgs e)
        {
            DynamicDataManager1.RegisterControl(DetailsView1);
            Session["jadefiniufoco"] = "Nao";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            table = DetailsDataSource.GetTable();
            Title = table.DisplayName;

        }

        protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            if (e.CommandName == DataControlCommands.CancelCommandName)
            {
                Response.Redirect(table.ListActionPath);
            }
        }

        protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            if (e.Exception == null || e.ExceptionHandled)
            {
                string codigoUsuarioMax = Request["codigoUsuario"].ToString();

                System.Data.SqlClient.SqlConnection sqle = new System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                sqle.Open();

                string sqlPegarL = "select * from usuario where codigoUsuario = " + codigoUsuarioMax;

                System.Data.SqlClient.SqlCommand cmdPegarL = new System.Data.SqlClient.SqlCommand(sqlPegarL, sqle);
                System.Data.SqlClient.SqlDataReader drPegarL = cmdPegarL.ExecuteReader();
                drPegarL.Read();

                string emailUsuarioMax = drPegarL["emailUsuario"].ToString();
                string nomeUsuario = drPegarL["nomeUsuario"].ToString();
                string codigoTipoUsuario = drPegarL["codigoTipoUsuario"].ToString();
                string senhaUsuario = drPegarL["senhaUsuario"].ToString();
                string ativoUsuario = drPegarL["ativoUsuario"].ToString();

                cmdPegarL.Dispose();
                drPegarL.Close();




                //Vamos gravar o registro da ação: log de atividades

                string data = DateTime.Now.ToString("dd/MM/yyyy");

                string hora = DateTime.Now.ToString("HH:mm:ss tt");

                string sqlInserirLog = "insert into log (codigoUsuario, descricaoLog, codigoEntidadeLog, dataLog, horaLog) values (" + Session["codigoUsuario"].ToString();

                sqlInserirLog = sqlInserirLog + ", 'Modificação de Usuário', " + Request["codigoUsuario"].ToString() + ", '" + data + "', '" + hora + "')";

                SqlConnection sqlLog = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                sqlLog.Open();

                SqlCommand inserirLog = new SqlCommand(sqlInserirLog, sqlLog);
                inserirLog.ExecuteNonQuery();

                inserirLog.Dispose();
                sqlLog.Close();

                //Gravação finalizada


                Response.Redirect(table.ListActionPath);
            }
        }
    }
}
